/*
 * Copyright (C) 2021 ShangHai Quicktron Intelligent Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为上海快仓智能科技开发研制。未经本公司正式书面同意，其他任何个人、团体
 * 不得使用、复制、修改或发布本软件.
 * 版权所有 翻版必究
 */

package ATOPSTD;

import java.io.File;
import java.util.function.Supplier;

/**
 * The type Able light api.
 */
public class AbleLightAPI {

    /**
     * The constant SUFFIX.
     */
    public static final String SUFFIX = ".dll";

    /**
     * LED显示管的值控制
     */
    final static byte NUMBER_DOT = 0;//显示数字的小数点位数

    /** 灯状态控制 */
    public static class LightStatus{
        // 灯光状态控制
        final static byte LIGHT_OFF = 0;//灯关闭
        final static byte LIGHT_ON = 1;//灯亮

        private final static byte LIGHT_FLASH_SPEED1 = 2;//灯闪烁 每2秒闪烁一次
        private final static byte LIGHT_FLASH_SPEED2 = 3;//灯闪烁 每1秒闪烁一次
        private final static byte LIGHT_FLASH_SPEED3 = 4;//灯闪烁 每0.5秒闪烁一次
        private final static byte LIGHT_FLASH_SPEED4 = 5;//灯闪烁 每0.25秒闪烁一次
    }

    public static class LightFlash{
        /**
         * 灯闪烁控制
         */
        final static short LIGHT_DEFAULT_FLASH_INTERVAL = 100;
        /**
         * The constant LIGHT_NOT_FLASH_INTERVAL.
         */
        final static short LIGHT_NOT_FLASH_INTERVAL = 0;
    }


    public static class LedStatus{
        /**
         * LED显示管的状态控制
         */
        public final static short LED_STAY_ON = 0;//保持常亮
        /**
         * The constant LED_DEFAULT_FLASH_INTERVAL.
         */
        public final static short LED_DEFAULT_FLASH_INTERVAL = -1;//以默认频率闪动
        /**
         * The constant LED_SATY_OFF.
         */
        public final static short LED_SATY_OFF = -2;//LED关闭
        /**
         * The constant ALL_SATY_OFF.
         */
        public final static short ALL_SATY_OFF = -3;//全部关闭
    }

    public static class ButtonStatus{
        /**
         * 确认按钮状态控制
         */
        final static byte UNLOCK = 0;//解锁拍灯按钮
        /**
         * The constant LOCK.
         */
        final static byte LOCK = 1;//锁定拍灯按钮
    }

    /** 电子标签所有按键配置 */
    public static class ButtonKeys{
        final static byte OK_KEY = 1;//确认键
    }

    //0 红色 1 绿色 2 黄色 3蓝色 4 紫 5 浅蓝
    private final static byte colorRed = 0;

    public static class BeepStatus{
        /**
         * 蜂鸣器状态控制
         */
        final static byte NOICE_ON = 1;//蜂鸣器打开
        /**
         * The constant NOICE_OFF.
         */
        final static byte NOICE_OFF = 0;//蜂鸣器关闭
    }




    /**
     * 初始化资源
     *
     * @param filePath the file path
     * @return boolean
     */
    public static boolean initResource(String filePath) {
        boolean success = true;
        try {
            loadResource(filePath);
        } catch (Exception e) {
            System.out.println("加载驱动失败");
            success = false;
        }
        return success;
    }


    /**
     * Init resource boolean.
     *
     * @param supplier the supplier
     * @return the boolean
     */
    public static boolean initResource(Supplier<String> supplier) {
        return initResource(supplier.get());
    }

    /**
     * 启动
     *
     * @param machineID the machine id
     * @return boolean
     */
    public static boolean start(int machineID) {
        boolean success = false;
        try {
            if (openAPI()) {
                openMachine(machineID);
                success = getMachineStatus(machineID);
            }
        } catch (Exception e) {
            System.out.println("打开PTL控制器失败");
        }
        return success;
    }

    /**
     * 关闭
     *
     * @param machineID the machine id
     * @return boolean
     */
    public static boolean stop(int machineID) {
        boolean success = false;
        try {
            closeMachine(machineID);
            success = closeAPI();
        } catch (Exception e) {
            System.out.println("关闭PTL控制器失败");
        }
        return success;
    }


    private static String getLibraryPath(String filePath, String fileName) {
        if (filePath.endsWith(SUFFIX)) {
            return filePath;
        }
        String libraryPath = filePath + File.separator + fileName;
        return libraryPath;
    }


    /**
     * 加载dll文件
     *
     * @throws Exception
     */
    private static void loadResource(String filePath) throws Exception {
        System.load(getLibraryPath(filePath, "dapapi.dll"));
        System.load(getLibraryPath(filePath, "JDapapi.dll"));
    }

    /**
     * 打开API,连接设备
     *
     * @throws Exception
     */
    private static boolean openAPI() throws Exception {
        int num = AbleAPI.AB_API_Open();
        return num > 0;
    }

    /**
     * 链接机器
     *
     * @param id
     */
    private static void openMachine(int id) {
        AbleAPI.AB_GW_Open(id);
    }

    /**
     * 获取机器状态是否链接成功
     *
     * @param id
     * @return
     */
    private static boolean getMachineStatus(int id) {
        int status = AbleAPI.AB_GW_Status(id);
        return status == 7;
    }

    /**
     * 关闭uAPI
     *
     * @return
     */
    private static boolean closeAPI() {
        int num = AbleAPI.AB_API_Close();
        return num > 0;
    }

    /**
     * 关闭机器
     *
     * @param id
     */
    private static void closeMachine(int id) {
        AbleAPI.AB_GW_Close(id);
    }

    /**
     * 锁定确认键
     *
     * @param machineID the machine id
     * @param lightNo   the light no
     */
    public static void button(int machineID, short lightNo,byte buttonStatus) {
        AbleAPI.AB_LB_SetLock(machineID, lightNo, ButtonStatus.LOCK, ButtonKeys.OK_KEY);
    }

    public static void setLock(int machineID, short lightNo,byte buttonStatus,byte buttonKeys) {
        //AbleAPI.AB_LB_SetLock(machineID, lightNo, ButtonStatus.LOCK, ButtonKeys.OK_KEY);
        AbleAPI.AB_LB_SetLock(machineID, lightNo, buttonStatus, buttonKeys);
    }

    /**
     * 解锁确认键
     *
     * @param machineID the machine id
     * @param lightNo   the light no
     */
    /*public static void unLock(int machineID, short lightNo) {
        AbleAPI.AB_LB_SetLock(machineID, lightNo, ButtonStatus.UNLOCK, ButtonKeys.OK_KEY);//解锁确认键
    }*/

    /**
     * 打开Led
     *
     * @param machineID the machine id
     * @param lightNo   the light no
     * @param num       the num
     */
    /*public static void ledOn(int machineID, short lightNo, int num) {
        AbleAPI.AB_LB_DspNum(machineID, lightNo, num, NUMBER_DOT, LedStatus.LED_STAY_ON);
    }*/

    public static void lednum(int machineID, short lightNo, int num,byte ledStatus) {
        //AbleAPI.AB_LB_DspNum(machineID, lightNo, num, NUMBER_DOT, LedStatus.LED_STAY_ON);
        AbleAPI.AB_LB_DspNum(machineID, lightNo, num, NUMBER_DOT, ledStatus);
    }

    /**
     * 关闭Led
     *
     * @param machineID the machine id
     * @param lightNo   the light no
     */
    /*public static void ledOff(int machineID, short lightNo) {
        AbleAPI.AB_LB_DspNum(machineID, lightNo, 0, NUMBER_DOT, LedStatus.LED_SATY_OFF);
    }*/

    /**
     * 打开Led 显示字符
     * @param machineID the machine id
     * @param lightNo the light no
     * @param str the d_str
     */
    public static void ledstr(int machineID, short lightNo, String str,byte ledStatus) {
        AbleAPI.AB_LB_DspStr(machineID, lightNo, str, NUMBER_DOT, ledStatus);
    }

    /**
     * LED闪烁
     *
     * @param machineID the machine id
     * @param lightNo   the light no
     * @param num       the num
     * @param interval  the interval
     */
/*    public static void ledFlash(int machineID, short lightNo, int num, short interval) {
        AbleAPI.AB_LB_DspNum(machineID, lightNo, num, NUMBER_DOT, interval);
    }*/

    /**
     * LED默认频率闪烁
     *
     * @param machineID the machine id
     * @param lightNo   the light no
     * @param num       the num
     * @param interval  the interval
     */
/*    public static void ledDefaultFlash(int machineID, short lightNo, int num, short interval) {
        ledFlash(machineID, lightNo, 0, LedStatus.LED_DEFAULT_FLASH_INTERVAL);
    }*/

    /**
     * 打开灯
     *
     * @param machineID the machine id
     * @param lightNo   the light no
     */
    public static void lightOn(int machineID, short lightNo) {
        AbleAPI.AB_LED_Status(machineID, lightNo, colorRed, LightStatus.LIGHT_ON);
        AbleAPI.AB_LED_Dsp(machineID, lightNo, LightStatus.LIGHT_ON, LightFlash.LIGHT_NOT_FLASH_INTERVAL);
    }

    public static void light(int machineID, short lightNo,byte lightStatus,short interval) {
        //AB_LED_Status 控制颜色
        AbleAPI.AB_LED_Dsp(machineID, lightNo, lightStatus, interval);
    }

    /**
     * 关闭灯
     *
     * @param machineID the machine id
     * @param lightNo   the light no
     */
    public static void lightOff(int machineID, short lightNo) {
        AbleAPI.AB_LED_Dsp(machineID, lightNo, LightStatus.LIGHT_OFF, LightFlash.LIGHT_NOT_FLASH_INTERVAL);
    }

    /**
     * 灯闪烁
     *
     * @param machineID the machine id
     * @param lightNo   the light no
     * @param interval  the interval
     */
    public static void lightFlash(int machineID, short lightNo, short interval) {
        AbleAPI.AB_LED_Dsp(machineID, lightNo, LightStatus.LIGHT_FLASH_SPEED4, interval);
    }

    /**
     * 以默认频率闪烁
     *
     * @param machineID the machine id
     * @param lightNo   the light no
     */
    public static void lightDefaultFlash(int machineID, short lightNo) {
        lightFlash(machineID, lightNo, LightStatus.LIGHT_FLASH_SPEED4);
    }

    /**
     * 蜂鸣器开关
     * @param machineID
     * @param lightNo
     * @param beepStatus
     */
    public static void beep(int machineID, short lightNo, byte beepStatus) {
        AbleAPI.AB_BUZ_On(machineID, lightNo, beepStatus);
    }

    /**
     * 接收消息
     *
     * @return tag rcv msg
     */
    public static TagRcvMsg recieveMessage() {
        TagRcvMsg tagRcvMsg = new TagRcvMsg();
        int num = AbleAPI.AB_Tag_RcvUIDMsg(tagRcvMsg);
        if (num > 0) {
            if (tagRcvMsg.subcmd == 6 || tagRcvMsg.subcmd == 7) {
                return tagRcvMsg;
            }
        }
        return null;
    }

}
